package com.book.lizhi.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.book.lizhi.AppApplication;
import com.book.lizhi.R;
import com.book.lizhi.constants.Constants;
import com.book.lizhi.http.FakeX509TrustManager;
import com.book.lizhi.util.AppLog;
import com.book.lizhi.util.Util;

public class SplashActivity extends Activity {

	private RelativeLayout mContainer;
	private ImageView mDefaultView;

	private boolean isQuit = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		mContainer = (RelativeLayout) findViewById(R.id.splash_container);
		mDefaultView = (ImageView) findViewById(R.id.default_splash);

		// new Handler().postDelayed(new Runnable() {
		//
		// @Override
		// public void run() {
		// startActivity(new Intent(getApplicationContext(), NavActivity.class));
		// finish();
		// }
		// }, 1000);
		checkShowBook();

	}

	private void checkShowBook() {
		AppApplication.instance.isCheck = Util.getBooleanBySharePreference(Constants.SHOW_BOOK);
		if (AppApplication.instance.isCheck) {
			FakeX509TrustManager.allowAllSSL();
			StringRequest request = new StringRequest("https://aygxylxk-config.firebaseio.com/isCheck3.json", new Listener<String>() {

				@Override
				public void onResponse(String arg0) {
					AppLog.e(arg0);
					String check = JSON.parse(arg0).toString();
					if ("false".equals(check)) {
						AppApplication.instance.isCheck = false;
						Util.saveDataBySharePreference(Constants.SHOW_BOOK, AppApplication.instance.isCheck);
					} else {
						AppApplication.instance.isCheck = true;
					}
					startActivity(new Intent(getApplicationContext(), NavActivity.class));
					finish();
					isQuit = true;
				}
			}, new ErrorListener() {

				@Override
				public void onErrorResponse(VolleyError arg0) {
					isQuit = true;
					startActivity(new Intent(getApplicationContext(), NavActivity.class));
					finish();
				}
			});
			AppApplication.instance.requestQueue.add(request);

		} else {
			new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {
					startActivity(new Intent(getApplicationContext(), NavActivity.class));
					isQuit = true;
					finish();
				}
			}, 1000);

		}
	}
}
